package com.jmb.waimao.common;

import lombok.Data;

import java.io.Serializable;

/**
 * Common response class
 * 
 * @param <T> Data type
 */
@Data
public class Result<T> implements Serializable {
    
    private static final long serialVersionUID = 1L;
    
    /**
     * Status code
     */
    private Integer code;
    
    /**
     * Message
     */
    private String message;
    
    /**
     * Data
     */
    private T data;
    
    /**
     * Success flag
     */
    private boolean success;
    
    /**
     * Private constructor
     */
    private Result() {}
    
    /**
     * Success response
     * 
     * @param <T> Data type
     * @return Result
     */
    public static <T> Result<T> success() {
        Result<T> result = new Result<>();
        result.setCode(200);
        result.setSuccess(true);
        result.setMessage("Success");
        return result;
    }
    
    /**
     * Success response with data
     * 
     * @param data Data
     * @param <T> Data type
     * @return Result
     */
    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.setCode(200);
        result.setSuccess(true);
        result.setMessage("Success");
        result.setData(data);
        return result;
    }
    
    /**
     * Success response with message and data
     * 
     * @param message Message
     * @param data Data
     * @param <T> Data type
     * @return Result
     */
    public static <T> Result<T> success(String message, T data) {
        Result<T> result = new Result<>();
        result.setCode(200);
        result.setSuccess(true);
        result.setMessage(message);
        result.setData(data);
        return result;
    }
    
    /**
     * Error response
     * 
     * @param <T> Data type
     * @return Result
     */
    public static <T> Result<T> error() {
        Result<T> result = new Result<>();
        result.setCode(500);
        result.setSuccess(false);
        result.setMessage("Error");
        return result;
    }
    
    /**
     * Error response with message
     * 
     * @param message Message
     * @param <T> Data type
     * @return Result
     */
    public static <T> Result<T> error(String message) {
        Result<T> result = new Result<>();
        result.setCode(500);
        result.setSuccess(false);
        result.setMessage(message);
        return result;
    }
    
    /**
     * Error response with code and message
     * 
     * @param code Status code
     * @param message Message
     * @param <T> Data type
     * @return Result
     */
    public static <T> Result<T> error(Integer code, String message) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setSuccess(false);
        result.setMessage(message);
        return result;
    }
}
